﻿
using Xacml.Core;
using System;
using System.Collections;
using System.Xml;

namespace Xacml.Core.Context
{
  public class ResourceElementReadWrite : TargetItemBase
  {
    private ResourceContentElementReadWrite _resourceContent;
    private ResourceScope _resourceScope;

    public virtual ResourceContentElementReadWrite ResourceContent
    {
      get
      {
        return this._resourceContent;
      }
      set
      {
        this._resourceContent = value;
      }
    }

    public bool IsHierarchical
    {
      get
      {
        return this._resourceScope != ResourceScope.Immediate;
      }
    }

    public virtual ResourceScope ResourceScopeValue
    {
      get
      {
        return this._resourceScope;
      }
      set
      {
        this._resourceScope = value;
      }
    }

    public ResourceElementReadWrite(ResourceContentElementReadWrite resourceContent, ResourceScope resourceScope, AttributeCollectionReadWrite attributes, XacmlVersion schemaVersion)
      : base(attributes, schemaVersion)
    {
      this._resourceContent = resourceContent;
      this._resourceScope = resourceScope;
    }

    public ResourceElementReadWrite(XmlReader reader, XacmlVersion schemaVersion)
      : base(reader, "Resource", schemaVersion)
    {
      foreach (AttributeElementReadWrite elementReadWrite1 in (CollectionBase) this.Attributes)
      {
        if (elementReadWrite1.AttributeId == "urn:oasis:names:tc:xacml:1.0:resource:scope")
        {
          foreach (AttributeValueElementReadWrite elementReadWrite2 in (CollectionBase) elementReadWrite1.AttributeValues)
          {
            if (elementReadWrite2.Contents != "Immediate")
            {
              this._resourceScope = (ResourceScope) Enum.Parse(typeof (ResourceScope), elementReadWrite2.Contents, false);
              return;
            }
          }
        }
      }
    }

    protected override void AttributeFound(string namespaceName, string attributeName, string attributeValue)
    {
    }

    protected override void NodeFound(XmlReader reader)
    {
      if (!(reader.LocalName == "ResourceContent"))
        return;
      this._resourceContent = new ResourceContentElementReadWrite(reader, this.SchemaVersion);
    }
  }
}
